package com.yuwen.controller;

import com.yuwen.pojo.Data;
import com.yuwen.service.DataService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//'http://localhost:8080'
@CrossOrigin(origins = "http://localhost:8080",allowCredentials = "true")
public class DataController {
    @Resource
    DataService dataService;

    // 批量添加数据
    @PostMapping("/upload")
    public Map<String,String> upload(MultipartFile excelFile) throws Exception{
        Map<String,String> map = new HashMap<>();
        if (excelFile.isEmpty()){
            map.put("mag","文件夹为空，重新上传");
            return map;
        }else {
            String fileName = excelFile.getOriginalFilename();
            InputStream is = excelFile.getInputStream();
            if (dataService.importDataInfo(fileName,is)){
                map.put("msg","数据添加成功");
                return map;
            }else {
                map.put("msg","数据添加失败，请重新添加");
                return map;
            }
        }
    }
    //===================================================================
    @RequestMapping("data/queryAll")
    public List<Data> queryAll(){
        return dataService.queryAll();
    }

    @RequestMapping("data/removeAll")
    public void removeAll(){
        dataService.removeAll();
    }

    @PostMapping("/data/add")
    public void addData(@RequestBody Data data){
        System.out.println(data);
        boolean add = dataService.add(data);
    }
    @RequestMapping("/data/query")
    public Data query(Long id){//http://localhost:8088/data/query?id=1
        return dataService.query(id);
    }//查不到

    @PostMapping("/data/update")
    public void update(@RequestBody Data data){
        System.out.println(data);
        dataService.update(data);
    }

    @RequestMapping("/data/querryAllY")
    public List<Data> querryAllY(double y){
        return dataService.findDataByY(y);
    }
    @RequestMapping("/data/remove")
    public void removeData(Long id){
        dataService.remove(id);
    }



}
